//
//  WarningNumberView.swift
//  Wanke
//
//  Created by BO ZHOU on 2020/10/27.
//

import UIKit

/**
 * 报警截图view
 */
open class WarningNumberView: UIView {

    lazy var warnNumberLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.textColor = UIColor.white
        label.font = UIFont.mediumFont(size: 10)
        return label
    }()
    
    lazy var gradientLayer: CAGradientLayer = {
        
        let gradientLayer: CAGradientLayer = CAGradientLayer()
        
        // 颜色分配
        gradientLayer.colors = [UIColor.init(hexString: "#FFF5819D").cgColor, UIColor.init(hexString: "#FFD84467").cgColor]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0 )
        gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        gradientLayer.locations = [0, 1]
        return gradientLayer
    }()
    
    
    // shapeLayer的宽度默认有一半在外面, 一半在里面, 所以计算半径的时候要缩小lineWidth*0.5, 保证所有都在self的内部
    lazy var shapeLayer: CAShapeLayer = {
        
        let shapeLayer = CAShapeLayer()
        return shapeLayer
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.addSublayer(gradientLayer)
    
        addSubview(warnNumberLabel)
    }
    
    required public init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        

        shapeLayer.frame = self.bounds

        warnNumberLabel.frame = self.bounds

        self.gradientLayer.frame = CGRect(x: 0, y: 0, width: self.width, height: self.height)
        gradientLayer.position = self.relativeCenter

    }
    

    public override func draw(_ rect: CGRect) {
        
        
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: height))
        path.addLine(to: CGPoint(x: 0, y: height/2))

        path.addArc(withCenter: CGPoint(x: height/2, y: height/2), radius: height/2, startAngle: CGFloat.pi, endAngle: CGFloat.pi + CGFloat.pi/2, clockwise: true)

        path.addLine(to: CGPoint(x: width - height/2, y: 0))
        path.addArc(withCenter: CGPoint(x: width - height/2, y: height/2), radius: height/2, startAngle: CGFloat.pi/2*3, endAngle: CGFloat.pi/2*3 + CGFloat.pi, clockwise: true)
        
        path.close()
        
        shapeLayer.path = path.cgPath
        self.layer.mask = shapeLayer
    }
}
